package cn.rokhdelar.umbrsapi.management.controller;

import cn.rokhdelar.umbrsapi.management.entity.SysPermission;
import cn.rokhdelar.umbrsapi.management.service.SysPermissionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/management/permissions")
public class SysPermissionController {
    private final SysPermissionService sysPermissionService;

    public SysPermissionController(SysPermissionService sysPermissionService) {
        this.sysPermissionService = sysPermissionService;
    }

    @GetMapping
    public Page<SysPermission> list(@RequestParam(value = "name", defaultValue = "") String name,
                                    @RequestParam(value = "current", defaultValue = "0") Integer current,
                                    @RequestParam(value = "size", defaultValue = "20") Integer size) {
        PageRequest pageRequest = PageRequest.of(current, size);
        return sysPermissionService.list(name, pageRequest);
    }

    @GetMapping(value = "/{id}")
    public SysPermission get(@PathVariable Long id) {
        return sysPermissionService.get(id);
    }

    @PostMapping
    public SysPermission create(@RequestBody SysPermission sysPermission) {
        return sysPermissionService.save(sysPermission);
    }

    @PatchMapping(value = "/{id}")
    public SysPermission update(@RequestBody SysPermission sysPermission) {
        return sysPermissionService.save(sysPermission);
    }

    @DeleteMapping(value = "/{id}")
    public void delete(@PathVariable Long id) {
        sysPermissionService.delete(id);
    }
}
